<?php
namespace app\article\controller;

use think\Request;
use app\common\controller\AdminBase;

class ApiAdminArticleUpload extends AdminBase
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
    }
    
    public function defaultUpload(Request $request)
    {
            
        $file = $request->file('fileDataFileName');
        $paramId = input('post.paramId');
        $paramField = input('post.paramField');
        if (empty($file)) {
            return jsonError('文件不存在');
        }
        $type = input('post.type');
        if (empty($type)) {
            $type = 'download';
        }
        if (SITE_HOST) {
            $public = 'public/';
        } else {
            $public = '';
        }
        if (!is_writable(PUBLIC_PATH . $this->settingInfo['uploadUrl'])) {
            return jsonError('/uploads 文件夹权限无法写入');
        }
        $name = 'uploads/' . $type;
        $aliyunossInfo = db('BaseAddons')->where('name','aliyunoss')->find();
        $qiniuossInfo = db('BaseAddons')->where('name','qiniuoss')->find();
        if ($aliyunossInfo || $qiniuossInfo) {
            if ($qiniuossInfo) {
                $title = uuid() . '.' . $ext;
                $res = model('addons\qiniuoss\model\QiniuossUploads')->defaultUpload($title,$tempContent,$name);
            }
            if ($aliyunossInfo) {
                $title = uuid() . '.' . $ext;
                $res = model('addons\aliyunoss\model\AliyunossUploads')->defaultUpload($title,$tempContent,$name);
            }
            if ($res['code'] == 1) {
                return jsonSuccess('ok',$res['data']);
            } else {
                return jsonError($res['msg']);
            }
        } else {
	        $uploadsPath  = PUBLIC_PATH . DS . $this->settingInfo['uploadUrl'] . DS . $type . DS . date('Y',time()) . DS . date('m',time()) . DS . date('d',time());
	        $info = $file->rule('uniqid')->validate(['ext'=>'zip,rar,jpg,png,gif'])->move($uploadsPath);
	        if ($info) {
	            $domain = $this->settingInfo['articleDomain'] ? config('domainStatic') : '';
	            $data = $domain . '/' . $public . $this->settingInfo['uploadUrl'] .'/'.$type.'/' . date('Y',time()) . '/' . date('m',time()) . '/' . date('d',time()) . '/' . $info->getFilename();
	            return jsonSuccess('',['url' => $data,'paramField' => $paramField, 'paramId' => $paramId]);
	        } else {
	            return jsonError($file->getError());
	        }
        }
    }

    
}